export function loginRequest(url,params){
	uni.request({
		url:url,
		method:'POST',
		data:params,
		success:res=>{
			if(res.data.code==='200'){
				localStorage.setItem('user',JSON.stringify(res.data.data))
				
				localStorage.setItem("Authorization",res.data.data.token)
				uni.showToast({
					icon:'success',
					title:'登录成功'
				})
				// 延时跳转
				setTimeout(() => {
					uni.switchTab({
						url: "/pages/home/home"
					})
				}, 1000)
			}else{
				uni.showToast({
					icon:'error',
					title:'登录失败，请检查账号或密码'
				})
			}
			
		}
	})
}

export function  updateProfile(url,params){
	let authorization=localStorage.getItem("Authorization")
	uni.request({
		url:url,
		data:params,
		header:{'Authorization':authorization},
		success:userDtoRes=>{
			if(userDtoRes.data.code==200){
				uni.showToast({
					icon:'success',
					title:userDtoRes.data.msg
				})
				localStorage.setItem("user",JSON.stringify(userDtoRes.data.data))
			}
		}
	})
}


export function  updateUserInfo(url,params,callBack){
	let authorization=localStorage.getItem("Authorization")
	return  uni.request({
		url:url,
		method:'post',
		header:{'Authorization':authorization},
		data:params,
		success:(res)=> {
			if(res.data.code==200){
				uni.showToast({
					icon:'success',
					title:'修改成功'
				})
				localStorage.setItem("user",JSON.stringify(res.data.data))
				console.log(res.data.data.username)
				callBack(res.data.data.username,res.data.data.desc)
				return res
			}
		}
	})
}

export function updateFile(url,res,userInfo,baseUrl){
	let authorization=localStorage.getItem("Authorization")
	uni.uploadFile({
		url:url,
		method:'POST',
		header:{'Authorization':authorization},
		name:"file",
		filePath:res.tempFilePaths[0],
		success:updaloadRes=>{
			let params={'profile':JSON.parse(updaloadRes.data).data,'id':userInfo.id}
			let url=baseUrl+'/user/updateProfile'
			updateProfile(url,params)
		},
		fail(f) {
			uni.showToast({
				icon:'error',
				title:'上传失败,请检查网络'
			})
		}
	})
}


